package newcoder_exp.justPractise;

import tree.TreeNode;

public class NC11 {
    //有序数组转为平衡二叉搜索树
    public class Solution {
        //二分取中点，中点就是子树的根
        public TreeNode sortedArrayToBST (int[] num) {
            return pre(0, num.length - 1, num);
        }

        public TreeNode pre(int left, int right, int[] nums) {
            if (left > right) return null;
            int mid = (right - left) / 2 + left;

            TreeNode root = new TreeNode(nums[mid]);
            root.left = pre(left, mid - 1, nums);
            root.right = pre(mid + 1, right, nums);
            return root;
        }
    }
}
